package com.guigu.servicegoods.controller;


import com.alibaba.fastjson.JSON;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.servicegoods.domain.Goods;
import com.guigu.servicegoods.domain.TbSpu;
import com.guigu.servicegoods.service.TbSpuService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/servicegoods/tbspu")
public class TbSpuController {

    @Autowired
    private TbSpuService spuService;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @PostMapping("fingPage")
    public Result findPage(@RequestBody(required = false) Map<String,Object> searchMap, Integer page, Integer size){
        return spuService.findPage(searchMap,page,size);
    }


    @PostMapping("insert")
    public Result insert(@RequestBody TbSpu spu){
        if(spuService.save(spu)){
            return Result.ok();
        }
        return Result.error();
    }

    @PostMapping("update")
    public Result update(@RequestBody TbSpu spu){
        if (spuService.updateById(spu)){
            if (spu.getIsMarketable() != "" && !"".equals(spu.getIsMarketable())){
                if (spu.getIsMarketable().equals("1")){
                    rabbitTemplate.convertAndSend("exchange.sj","", spu.getId());
                }
                if ((spu.getIsMarketable().equals("0"))){
                    rabbitTemplate.convertAndSend("exchange.xj","",spu.getId());
                }
            }
            return Result.ok();
        }
        return Result.error();
    }

    @GetMapping("delete")
    public Result delete(String id){
        return spuService.delete(id);
    }

    @GetMapping("findById")
    public Result findById(String id){
        TbSpu spu = spuService.getById(id);
        if (spu == null){
            return Result.error();
        }
        return Result.ok().data("spu",spu);
    }

    @PostMapping("/saveGoods")
    public Result saveGoods(@RequestBody Map<String, Object> goods) {
        System.out.println(goods.get("skuList"));
        String str = JSON.toJSONString(goods);
        Goods goods2 = JSON.parseObject(str, Goods.class);
        System.out.println(goods);
        System.out.println(goods2);
        TbSpu spu = goods2.getSpu();
        spu.setFreightId(0);
        spu.setSaleNum(0);
        spu.setCommentNum(0);
        spu.setIsMarketable("0");
        spu.setIsEnableSpec("1");
        spu.setIsDelete("0");
        spu.setStatus("0");
        goods2.setSpu(spu);
        spuService.saveGoods(goods2);
        return Result.ok();
    }

    @GetMapping("findGoodsById")
    public Goods findGoodsById(String id){
        return spuService.findGoodsById(id);
    }


    @GetMapping("checkGoods")
    public Result checkGoods(String id,String status){
        TbSpu spuCheck = spuService.getById(id);
        spuCheck.setStatus(status);
        boolean checkUpdate = spuService.updateById(spuCheck);
        if (checkUpdate)
            return Result.ok();
        return Result.error();
    }

//    @PostMapping("findGoodsPage")
//    public Result findGoodsPage(@RequestBody(required = false) Map<String,Object> searchMap, Integer page, Integer size) {
//        return spuService.findGoodsPage(searchMap,page,size);
//    }
}

